home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12716 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: beta.nedernet.nl!usenet
  2. From: jos@and.nl (Jos A. Horsmeier)
  3. Newsgroups: comp.lang.c,comp.lang.perl
  4. Subject: Re: Random File Access - I don't get it
  5. Date: 2 Apr 1996 13:02:04 GMT
  6. Organization: AND Operations Research B.V.
  7. Message-ID: <4jr8gc$l38@beta.nedernet.nl>
  8. References: <3160DE1E.495C@teleport.com>
  9. NNTP-Posting-Host: klepzeiker.and.nl
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <3160DE1E.495C@teleport.com>, kinards@teleport.com wrote:
  15.  
  16. |  As I was coding a simple program to randomly read lines from a text file 
  17. |it occurred to me something was amiss. This is probably some fundamental 
  18. |oversight on my part, but some illumination would be helpful...
  19. |
  20. |  Suppose I have two files, one contains text (1 line of random length text 
  21. |per 'record') and another which is my index into the text file, which contains
  22. |the starting and ending byte positions in the text file for each 'record'. Now 
  23. |the question..
  24. |
  25. |Suppose the text file has 2,000,000 entries. Now, what's the difference in reading 
  26. |1,000,000 lines from the index file to find the starting and ending byte positions
  27. |in the text file for record 1,000,000, and then seeking these in the text file, 
  28. |rather than just reading 1,000,000 times from the text file to get the same 'record'
  29. |in the first place?
  30.  
  31. The difference is, that there's (normally) no need to read the index file sequentially.
  32. Finding the appropriate entry in the index file can be as simple as:
  33.  
  34.     typedef struct {
  35.         long offset;
  36.         long end;
  37.     } index_t;
  38.  
  39.     index_t idx;
  40.  
  41.     fseek(file, 1000000*sizeof(index_t), 0);
  42.     fread(&index, sizeof(index_t), 1, file);
  43.  
  44. If you _do_ have to read an index file sequentially (which is silly in most cases),
  45. still then there can be a slight speed gain if the data records happen to be very
  46. large, e.g. reading 1,000,000 index entries can be way faster than reading the same
  47. number of records of, say, 64Kb each.
  48.  
  49. kind regards,
  50.  
  51. Jos aka jos@and.nl
  52. -- 
  53. Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
  54.  
  55.